package com.application.fabric.users.config.Interceptor;

import com.application.fabric.users.config.Dealmsg;
import com.application.fabric.users.config.encryptAl.TokenVer;
import com.application.fabric.users.entity.ExtractToken;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private Dealmsg dealmsg;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入拦截器");

//      token空zhi判读
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token == null || !token.startsWith("Bearer ")) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.getWriter().write(dealmsg.TokenIsNull().toString());
            System.out.println(token);
            System.out.println("token不合法");
            return false;
        }
        token = token.substring(7);

//        token过期校验
        if (isOutTimeToken(token)){
            response.getWriter().write(dealmsg.Tokentimeoutmsg().toString());
            System.out.println("token过期");
            return false;
        }
        isValidToken(token);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("控制执行完毕");
        System.err.println("================================== postHandle1 ===========================================");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("得到返回结果");
        System.err.println("================================== afterCompletion1 ===========================================");
    }

//    token认证
    @Resource
    public ExtractToken extractToken;
    @Autowired
    private TokenVer tokenmethod;
//    非法判断
    private boolean isValidToken(String token) {
    //    获取token里面信息 去数据库查询请求
        Claims claims = tokenmethod.checkJWT(token);
        String phone = claims.get("userPhone").toString();
        String publickey = claims.get("userKey").toString();
        String userid = claims.get("userid").toString();
////    查询数据库phone信息
//        // 信息提取
        extractToken.setPhonenum(phone);
        extractToken.setPublickey(publickey);
        extractToken.setUserid(userid);
        return true;
    }

//    token过期验证
    private boolean isOutTimeToken(String token){
        Date now =  new Date();
        Date OutTime = tokenmethod.checkJWT(token).getExpiration();
//        过期判断
        if (now.compareTo(OutTime) < 0){
            return false;
        }
        return true;
    }


}
